package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.PersonProxy;
import com.rfid.project.service.PersonProxyService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;

@Controller
public class PersonProxyController {

	private static final Logger logger = LoggerFactory
			.getLogger(PersonProxyController.class);

	@Autowired
	private PersonProxyService personProxyService;

	@Autowired
	private PeopleService peopleService;

	@RequestMapping(value = "/personProxymgr")
	public String personProxymgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People> persons = peopleService.findAll();
		model.addAttribute("persons", persons);
		return "personProxymgr";
	}

	@RequestMapping(value = "/queryPersonProxy")
	public String queryPersonProxy(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "personId", defaultValue = "-1") Long personId,
			@RequestParam(value = "agentId", defaultValue = "-1") Long agentId,
			@RequestParam(value = "dateCreatedBegin", defaultValue = "") Date dateCreatedBegin,
			@RequestParam(value = "dateCreatedEnd", defaultValue = "") Date dateCreatedEnd,
			@RequestParam(value = "startDateBegin", defaultValue = "") Date startDateBegin,
			@RequestParam(value = "startDateEnd", defaultValue = "") Date startDateEnd,
			@RequestParam(value = "endDateBegin", defaultValue = "") Date endDateBegin,
			@RequestParam(value = "endDateEnd", defaultValue = "") Date endDateEnd,
			@RequestParam(value = "withdrawDateBegin", defaultValue = "") Date withdrawDateBegin,
			@RequestParam(value = "withdrawDateEnd", defaultValue = "") Date withdrawDateEnd,
			@RequestParam(value = "status", defaultValue = "") String status,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<PersonProxy> personProxys = personProxyService.findAll(personId,
				agentId, dateCreatedBegin, dateCreatedEnd, startDateBegin,
				startDateEnd, endDateBegin, endDateEnd, withdrawDateBegin,
				withdrawDateEnd, status, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", personProxys.getTotalElements());
		JSONArray models = new JSONArray();
		for (PersonProxy item : personProxys) {
			JSONObject mod = new JSONObject();
			wrapData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	@RequestMapping(value = "/findPersonProxy")
	public String findPersonProxy(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		PersonProxy personProxy = personProxyService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(personProxy, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(PersonProxy item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("personId", item.getPerson().getId());
		mod.put("personName", item.getPerson().getName());
		mod.put("agentId", item.getAgent().getId());
		mod.put("agentName", item.getAgent().getName());
		mod.put("dateCreated",	com.rfid.project.util.DateUtil.dateTheDayToString(item.getDateCreated()));
		mod.put("startDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getStartDate()));
		mod.put("endDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getEndDate()));
		mod.put("withdrawDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getWithdrawDate()));
		mod.put("status", item.getStatus());
	}

	@RequestMapping(value = "/editPersonProxy", method = RequestMethod.POST)
	public String editPersonProxy(HttpServletRequest request,
			HttpServletResponse response, @Valid PersonProxy personProxy,
			Model model, RedirectAttributes redirectAttributes) {
		personProxyService.savePersonProxy(personProxy);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delPersonProxy")
	public String delPersonProxy(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.personProxyService.deletePersonProxy(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
